package com.project.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.SupplierBean;
import com.project.bean.SupplyInfoBean;
import com.project.dto.SupplyRefundDTO;
import com.project.dto.SupplyRefundRecordDto;
import com.project.mapper.IInventoryInfoMapper;
import com.project.mapper.IInventoryMapper;
import com.project.mapper.ISupplierMapper;
import com.project.mapper.ISupplyInfoMapper;
import com.project.service.ISupplierServiceRE;
import com.project.util.SnowIdUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;

@Service
@Transactional
public class SupplierServiceImpRE implements ISupplierServiceRE {
    @Autowired
    private ISupplierMapper supplierMapper;
    @Autowired
    private IInventoryInfoMapper inventoryInfoMapper;
    @Autowired
    private ISupplyInfoMapper supplyInfoMapper;
    @Autowired
    private IInventoryMapper inventoryMapper;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public Boolean addSupplierReturnOrder(String supplierInfoId,String reason) {
        //先在供货批次信息表中，根据id获取，供应商编号
        QueryWrapper<SupplyInfoBean> qw = new QueryWrapper<SupplyInfoBean>();
        qw.eq("supplyId", supplierInfoId);
        SupplyInfoBean supplyInfoBean = supplyInfoMapper.selectOne(qw);
        QueryWrapper<SupplierBean> qwver = new QueryWrapper<SupplierBean>();
        qwver.eq("supplierReturnId", supplierInfoId);
        SupplierBean supplierBeanJudge = supplierMapper.selectOne(qwver);
        if (supplyInfoBean != null) {
            if (supplierBeanJudge != null){
                return false;
            }
            //建立、保存供应商退货对象
            SupplierBean supplierBean = new SupplierBean();
            supplierBean.setSupplierReturnId(SnowIdUtils.uniqueLongHex());//退货订单编号
            supplierBean.setSupplyRef(supplyInfoBean.getSupplyRefId());//供应单编号
            supplierBean.setDate(LocalDateTime.now());//退货时间
            supplierBean.setOrderMoney(supplyInfoBean.getAllMoney());//总金额
            supplierBean.setStatus(supplyInfoBean.getStatus());//订单状态
            supplierBean.setVerifyStatus(0);//审核状态0--待审核  1---审核通过 2 ----审核不通过
            supplierBean.setReason(reason);
            supplierMapper.insert(supplierBean);

            SupplyRefundRecordDto supplyRefundRecordDto = new SupplyRefundRecordDto();
            supplyRefundRecordDto.setRefundRef(supplierBean.getSupplierReturnId());
            supplyRefundRecordDto.setBatch(supplierInfoId);
            supplyRefundRecordDto.setReason(reason);
            supplyRefundRecordDto.setDate(supplierBean.getDate());
            supplyRefundRecordDto.setOrderMoney(supplierBean.getOrderMoney());

            String message=JSON.toJSONString(supplyRefundRecordDto);
            rabbitTemplate.convertAndSend("BackPurchaseApply","rejected.key",message);

            return true;
        }
        return false;
    }

    @Override
    public Boolean updateSupplierStatus(String supplierReturnId, int status) {
        QueryWrapper<SupplierBean> qw = new QueryWrapper<SupplierBean>();
        qw.eq("supplierReturnId", supplierReturnId);
        SupplierBean supplierBean = supplierMapper.selectOne(qw);
        if (supplierBean != null){
            supplierBean.setVerifyStatus(status);//审核状态0--待审核  1---审核通过 2 ----审核不通过

            supplierMapper.updateById(supplierBean);


//            SupplyRefundDTO dto = SupplyRefundDTO.fromSupplierBean(supplierBean);
            // 将 dto 对象发送给 B 服务器
//            rabbitTemplate.convertAndSend("BackPurchaseApply","rejected.key",dto);
            return true;
        }
        return false;
    }

    @Override
    public IPage<SupplierBean> findSupplierBean(int pageNO) {
        // 构造查询分页参数
        Page<SupplierBean> page = new Page<>(pageNO, 3);

        IPage<SupplierBean> supplierBeanIPage = supplierMapper.selectPage(page, null);

        return supplierBeanIPage;
    }
}